package com.openlogic.logic.controller.datasource;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Get;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.annotation.Path;
import org.noear.solon.auth.annotation.AuthPermissions;
import org.noear.solon.core.handle.Result;

import com.mybatisflex.core.paginate.Page;
import com.openlogic.common.core.enums.BusinessType;
import com.openlogic.common.core.web.controller.BaseController;
import com.openlogic.common.core.web.domain.PageData;
import com.openlogic.common.datasource.annotation.QzLog;
import com.openlogic.datasource.api.entity.DataColumnEntity;
import com.openlogic.datasource.api.entity.DataTableEntity;
import com.openlogic.datasource.api.query.DataColumnQuery;
import com.openlogic.datasource.api.query.DataTableQuery;
import com.openlogic.datasource.api.vo.DataColumnVo;
import com.openlogic.datasource.api.vo.DataTableVo;
import com.openlogic.datasource.api.vo.DataTreeVo;
import com.openlogic.datasource.api.vo.SqlConsoleVo;
import com.openlogic.logic.mapstruct.DataColumnMapperStruct;
import com.openlogic.logic.mapstruct.DataTableMapperStruct;
import com.openlogic.logic.service.datasource.DataColumnService;
import com.openlogic.logic.service.datasource.DataSourceService;
import com.openlogic.logic.service.datasource.DataTableService;

/**
 * @ClassName: MetadataController
 * @Description: 元数据信息Controller层
 * @author weiyongpeng
 * @date 2024年11月9日 上午10:53:50
 */
@Controller
@Mapping("/metadata")
public class MetadataController extends BaseController {

	@Inject
	private DataSourceService dataSourceService;

	@Inject
	private DataTableService dataTableService;

	@Inject
	private DataTableMapperStruct dataTableMapperStruct;
	
	@Inject
	private DataColumnService dataColumnService;
	
	@Inject
	private DataColumnMapperStruct dataColumnMapperStruct;

	/**
	 * 描述：获取层级树
	 * @Title: getDataMetadataTree
	 * @param level
	 * @param metadataColumnQuery
	 * @return
	 * @author weiyongpeng
	 * @date 2024年11月13日 上午8:53:51
	 */
	@QzLog(title = "获取层级树", businessType = BusinessType.OTHER)
	@AuthPermissions("rule:datasource:other")
	@Get
	@Mapping("/tree/{level}")
	public Result<Map<String, List<DataTreeVo>>> getDataMetadataTree(@Path String level,
			DataColumnQuery dataColumnQuery) {
		Map<String, List<DataTreeVo>> list = dataSourceService.getDataMetadataTree(level, dataColumnQuery);
		return Result.succeed(list);
	}
	
	/**
	* 描述：获取sql控制台数据源层级树
	* @Title: getDataSourceTree
	* @param dataColumnQuery
	* @return
	* @author weiyongpeng
	* @date  2024年11月25日 上午9:56:08
	 */
	@QzLog(title = "获取sql控制台数据源层级树", businessType = BusinessType.OTHER)
	@AuthPermissions("rule:datasource:other")
	@Get
	@Mapping("/tree/sqlConsole")
	public Result<List<DataTreeVo>> getSqlConsoleTree(DataColumnQuery dataColumnQuery) {
		List<DataTreeVo> list = dataSourceService.getSqlConsoleDataTree(dataColumnQuery);
		return Result.succeed(list);
	}

	/**
	* 描述：根据数据表的ID查询数据表详情
	* @Title: getDataMetadataTableById
	* @param id
	* @return
	* @author weiyongpeng
	* @date  2024年11月13日 下午5:59:17
	 */
	@QzLog(title = "根据数据表的ID查询数据表详情", businessType = BusinessType.LIST)
	@AuthPermissions("rule:datasource:other")
	@Get
	@Mapping("/view/{id}/{tableId}/table")
	public Result<DataTableVo> getDataMetadataTableById(@Path String id,@Path String tableId) {
		return Result.succeed(dataTableService.getMetadataTableById(id,tableId));
	}

	/**
	 * 描述：根据数据源的id来获取指定数据库表
	 * @Title: getDbTables
	 * @param id
	 * @return
	 * @author weiyongpeng
	 * @date 2024年11月10日 下午5:04:40
	 */
	@QzLog(title = "根据数据源的id来获取指定数据库表", businessType = BusinessType.LIST)
	@AuthPermissions("rule:datasource:list")
	@Get
	@Mapping("/list/tables")
	public PageData<DataTableVo> getDataMetadataTableList(Page<DataTableEntity> page, DataTableQuery dataTableQuery) {
		startPage(page);
		List<DataTableEntity> list = dataTableService.getDataMetadataTableList(dataTableQuery);
		List<DataTableVo> collect = list.stream().map(dataTableMapperStruct::toVO).collect(Collectors.toList());
		return getPageData(collect);
	}
	
	/**
	* 描述：数据表结果预览
	* @Title: getTableDataList
	* @param dataTableQuery
	* @return
	* @author weiyongpeng
	* @date  2024年11月24日 下午3:50:56
	 */
	@QzLog(title = "数据表结果预览", businessType = BusinessType.LIST)
	@AuthPermissions("rule:datasource:list")
	@Get
	@Mapping("/list/tableDatas")
	public Result<List<SqlConsoleVo>> getTableDataList(DataTableQuery dataTableQuery) {
		return Result.succeed(dataTableService.getTableDataList(dataTableQuery));
	}

	/**
	* 描述：获取数据表详细信息
	* @Title: getDataMetadataColumnById
	* @param id
	* @param tableId
	* @param columnId
	* @return
	* @author weiyongpeng
	* @date  2024年11月13日 下午8:50:24
	 */
	@QzLog(title = "获取数据表详细信息", businessType = BusinessType.LIST)
	@AuthPermissions("rule:datasource:list")
	@Get
	@Mapping("view/{id}/{tableId}/{columnId}/column")
	public Result<DataColumnVo> getDataMetadataColumnById(@Path String id, @Path String tableId, @Path String columnId) {
        return Result.succeed(dataColumnService.getMetadataColumnById(id, tableId, columnId));
	}

	/**
	 * 描述：数据库表字段详情
	 * @Title: getDbTableColumns
	 * @param id
	 * @param tableName
	 * @return
	 * @author weiyongpeng
	 * @date 2024年11月12日 下午5:01:52
	 */
	@QzLog(title = "数据库表字段详情", businessType = BusinessType.LIST)
	@AuthPermissions("rule:datasource:list")
	@Get
	@Mapping("/list/columns")
	public PageData<DataColumnVo> getDbTableColumns(Page<DataColumnEntity> page, DataColumnQuery dataColumnQuery) {
		startPage(page);
		List<DataColumnEntity> list = dataColumnService.getDataMetadataColumnList(dataColumnQuery);
        List<DataColumnVo> collect = list.stream().map(dataColumnMapperStruct::toVO).collect(Collectors.toList());
        return getPageData(collect);
	}
	
}
